<?php

declare (strict_types = 1);

namespace app\common\service\system;

use app\common\service\BaseService;
use app\common\exception\AdminException;
use app\common\repository\system\SysConfigGroupRepository;
use think\facade\Cache;

class SysConfigGroupService extends BaseService
{
    const CACHE_TAG = 'system_config_tag';

    public function __construct(SysConfigGroupRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 获取配置分类列表
     *
     * @param array $where
     * @return \think\Collection|array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(array $where = []): \think\Collection|array
    {
        return $this->repository->getByWhere($where, ['sort' => 'desc'])->toArray();
    }

    /**
     * 获取配置分类详情信息
     *
     * @param $id
     * @return \app\common\model\BaseModel|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo($id): ?\app\common\model\BaseModel
    {
        return $this->repository->getOne($id);
    }

    /**
     * 创建配置分类信息
     *
     * @param array $data
     * @return \app\common\model\BaseModel|array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function create(array $data): \app\common\model\BaseModel|array
    {
        $data = [
            'name' => $data['name'],
            'mark' => $data['mark'],
            'sort' => $data['sort'],
        ];
        return $this->repository->save($data);
    }

    /**
     * 更新配置分类信息
     *
     * @param array $data
     * @param $id
     * @return bool|int
     */
    public function update(array $data, $id): bool|int
    {
        if(empty($id)) {
            throw new AdminException('ID 不能为空');
        }
        $data = [
            'name' => $data['name'],
            'mark' => $data['mark'],
            'sort' => $data['sort'],
        ];
        return $this->repository->update($data, $id);
    }

    /**
     * 删除配置分类信息
     *
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete($id): bool
    {
        return $this->repository->delete($id);
    }


    /**
     * 通过分组标识获取配置项
     *
     * @param $mark
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGroupConfigs($mark): mixed
    {
        $cacheKey = 'system_config_group:' . $mark;

        // 尝试从缓存中获取整个列表
        $configs = Cache::get($cacheKey);

        if (empty($configs) || !is_array($configs)) {
            // 如果缓存不存在或不是数组，则从数据库查询并缓存
            $configs = $this->repository->getGroupConfigs($mark)->toArray();
            foreach ($configs['configs'] as &$config) {
                if(!empty($config['options'])) {
                    if (is_string($config['options'])) {
                        $config['options'] = json_decode($config['options'], true);
                    }
                }
            }
            Cache::tag(self::CACHE_TAG)->set($cacheKey, $configs, 3600); // 缓存时间设置为1小时
        }

        return $configs;
    }


    /**
     * 用mark查询id
     *
     * @param $mark
     * @return mixed
     */
    public function getIdByMark($mark): mixed
    {
        return $this->repository->getValue('id', ['mark' => $mark]);
    }
}